package com.mengwen.backstage.controller.admin;

import com.mengwen.backstage.pojo.*;
import com.mengwen.backstage.service.PcUserService;
import com.mengwen.backstage.service.RankService;
import com.mengwen.backstage.util.GetUserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author LiangYonghui
 * @date 2020/4/20 23:13
 * @description 排行榜controller
 */
@Controller
@Slf4j
public class RankController {

    @Autowired
    RankService rankService;

    @Autowired
    PcUserService pcUserService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    GetUserInfoUtil getUserInfoUtil;

    /**
     * String ==> unionid
     * Double ==> cover money or set money
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batch_add/{type}",method = RequestMethod.POST)
    public Result batchAdd(@RequestBody Map<String, Double> map, @PathVariable("type") int type) {
        if (Type.One == type) {
            rankService.batchAdd(RankKeyName.ANSWER_DAY_KEY, map);
            rankService.batchAdd(RankKeyName.ANSWER_WEEK_KEY, map);
            rankService.batchAdd(RankKeyName.ANSWER_MONTH_KEY, map);
        }else if (Type.Two == type) {
            rankService.batchAdd(RankKeyName.QUESTION_DAY_KEY, map);
            rankService.batchAdd(RankKeyName.QUESTION_WEEK_KEY, map);
            rankService.batchAdd(RankKeyName.QUESTION_MONTH_KEY, map);
        }

        return new Result(true,"批量添加成功");
    }


    /**
     * type 1 小程序
     * type 2 pc
     * @param unionid
     * @param money
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/increment/{type}")
    public Result incrementMoney(String unionid, double money,@PathVariable("type") int type){
        if (Type.One == type) {
            rankService.incrementMoney(RankKeyName.ANSWER_DAY_KEY, unionid, money);
            rankService.incrementMoney(RankKeyName.ANSWER_WEEK_KEY, unionid, money);
            rankService.incrementMoney(RankKeyName.ANSWER_MONTH_KEY, unionid, money);

        } else if (Type.Two == type) {
            rankService.incrementMoney(RankKeyName.QUESTION_DAY_KEY, unionid, money);
            rankService.incrementMoney(RankKeyName.QUESTION_WEEK_KEY, unionid, money);
            rankService.incrementMoney(RankKeyName.QUESTION_MONTH_KEY, unionid, money);
        }


        return new Result(true,"增加成功");
    }

    /**
     * 这里到了根据获取到的信息去查询信息的地步了
     * 就是封装那些信息，本来没那么麻烦的
     * @return
     * @throws Exception
     *
     * type 1 小程序
     *      0 pc
     */
    // 测试用
    @ResponseBody
    @RequestMapping("/list_rank")
    public List<Chart> listRank() throws Exception {
        // 从缓存中获取money和unionid
        Set<ZSetOperations.TypedTuple<String>> typedTuples = rankService.listRank(RankKeyName.QUESTION_DAY_KEY, 0, 10);

        Iterator<ZSetOperations.TypedTuple<String>> iterator = typedTuples.iterator();

        List<Chart> charts = new ArrayList<>();

        List<String> openids = new LinkedList<>();
        // 防止空指针 初始化十个：
        for (int j = 0; j < 10; j++) {
            openids.add("");
        }

        // 根据unionid获取openid
        List<WeixinUser> list = null;
        // 记录money
        List<Double> moneys = new LinkedList<>();
        int i = 1;
        while (iterator.hasNext()) {
            ZSetOperations.TypedTuple<String> next = iterator.next();
            String unionid = next.getValue(); // unionid
            Double money = next.getScore(); // money
            moneys.add(money);
            log.info("一一对应关系2：");
            log.info(unionid + ":" + money);
            // 根据unionid查询openid
            String openid = pcUserService.findOpenidByUnionid(unionid);
            log.info("获取到的openid:");
            log.info(openid);
            if ("".equals(openid) || null == openid) {
                log.info("unionid :" + unionid + "对应的openid为空");
            } else {
                // 将前十或十以内的openid存起来
                System.out.println("这里的i:" + i);
                openids.add(i - 1, openid);
                i++;
            }

        }

        log.info("---------------openids----------------");

        // 还得记录一下money
        log.info("" + openids);

        // 将列表中的openid转化为字符串数组
        String[] strs1 =openids.toArray(new String[openids.size()]);


        // 获取openid字符串数组对应的用户信息
        list = getUserInfoUtil.getTenValueByKeyAndOpenid(strs1);

;
        for (int z = 0; z < i - 1; z++) {
            String imgurl = list.get(z).getHeadimgurl();
            String nickname = list.get(z).getNickname();
            charts.add(new Chart(nickname,imgurl,moneys.get(z),z + 1));
        }


        log.info("------------------------------------------");
        log.info( "" +charts);
        log.info("-------------------------------------");

        return charts;
    }
}





















